package com.jiazhong.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 编码过滤器
 */
@WebFilter("/*")
public class EncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) arg0;
        HttpServletResponse response=(HttpServletResponse) arg1;

        //设置请求的编码及
        request.setCharacterEncoding("UTF-8");
        //设置响应类型及编码集
        response.setContentType("text/html;charset=UTF-8");
//        System.out.println("EncodingFilter拦截到请求");
        chain.doFilter(request,response);
//        System.out.println("EncodingFilter拦截到响应");
    }
}
